
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>商户管理</cite></a>
    <a><cite>所有商户</cite></a>
  </div>
</div>
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-tab layui-tab-brief" lay-filter="app-content-list">
      <ul class="layui-tab-title">
        <li class="layui-this">商户列表</li>
      </ul>
      <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
          <div class="layui-row">
            <div class="layui-form" style="float:right;">
              <div class="layui-form-item" style="margin:0;">
                <label class="layui-form-label">商户ID</label>
                <div class="layui-input-inline">
                  <input type="text" name="mchId" id="mchId" placeholder="商户ID" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">商户类型</label>
                <div class="layui-input-inline">
                  <select name="type" id="type" lay-search="">
                    <option value="">请选择商户类型</option>
                    <option value="1">平台账户</option>
                    <option value="2">私有账户</option>
                  </select>
                </div>
                <button id="search" class="layui-btn layuiadmin-btn-list" data-type="reload">
                  <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                </button>
              </div>
            </div>
          </div>

      <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
          <button class="layui-btn layuiadmin-btn-list" data-type="add">添加</button>
        </div>
        <table id="Pay_Mgr_Merchant_dataAll" lay-filter="Pay_Mgr_Merchant_dataAll"></table>
      </div>
    </div>
  </div>
</div>
  </div>
</div>

<script type="text/html" id="payBar">
  <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="merchantDetail">查看商户</a>
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="accountDetail">查看账户</a>
  <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="passageConfig">通道配置</a>
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
</script>

<script>
    layui.use(['admin',  'table','form','util'], function(){
        var $ = layui.$
            ,admin = layui.admin
            ,view = layui.view
            ,table = layui.table
            ,form = layui.form
            ,layer =layui.layer;

        form.render(null,'app-content-list');

        var tplStatus = function(d){
            if(d.state == 1) {
                return "<span class='layui-btn layui-btn-xs'>启用</span>";
            }
            return "<span class='layui-btn layui-btn-xs layui-btn-disabled'>停止</span>";
        };

        var tplType = function(d){
            if(d.kind == 1) {
                return "<span style='color: indigo'>平台账户</span>";
            }else if(d.kind == 2) {
                return "<span style='color: coral'>私有账户</span>";
            }
        };

        //商户列表
        table.render({
            elem: '#Pay_Mgr_Merchant_dataAll'
            ,url: layui.setter.baseUrl + '/mch_info/list' //列表接口
            ,where: {
                access_token: layui.data(layui.setter.tableName).access_token
            }
            ,id: 'tableReload'
            ,page: true
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'mchId', title: '商户ID'}
                ,{field: 'name', title: '商户名称'}
                ,{field: 'kind', title: '商户类型', templet: tplType}
                ,{field: 'state', title: '状态', templet: tplStatus}
                ,{field: 'createTime', title: '创建时间', width: 200, templet: '<div>{{ layui.util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss") }}</div>'}
                ,{field: 'edit', title: '操作', width: 320, toolbar: '#payBar' }
            ]]
            ,skin: 'line'
        });

        //监听工具条
        table.on('tool(Pay_Mgr_Merchant_dataAll)', function(obj) {

            var data = obj.data;

            if (obj.event === 'merchantDetail') {
                // 1 ajax 查最新数据，
                var mchId = data.mchId;
                admin.req({
                    type: 'get',
                    url: layui.setter.baseUrl + '/mch_info/get',
                    data: {
                        mchId : mchId
                    },
                    error: function(err){
                        layer.alert(JSON.stringify(err.field), {
                            title: '错误提示'
                        })
                    },
                    success: function(res){
                        if(res.code == 0){

                            // 2 赋值
                            data =res.data;

                            admin.popup({
                                title: '查看商户'
                                , area: ['390px', '540px']
                                , id: 'LAY-popup-content-view'
                                , success: function (layero, index) {
                                    view(this.id).render('merchant/list/mch_view').done(function () {
                                        form.render(null, 'layuiadmin-app-form-list');
                                        var body =layer.getChildFrame('body',index);
                                        body.find('#email').val(data.email);
                                        console.log(body)
                                        console.log(data.email)
                                        form.val("layuiadmin-app-form-list", {

                                            "mchId" : data.mchId,
                                            "type" : data.kind==1?"平台账户":"私有账户",
                                        })

                                            $('#name').val(data.name);
                                            if(data.kind == 1) {
                                                $('#type').val("平台账户");
                                            }else if(data.kind == 2) {
                                                $('#type').val("私有账户");
                                            }
                                            $('#mchRate').val(data.mchRate);
                                            $('#email').val(data.email);
                                            $('#mobile').val(data.mobile);
                                            if(res.data.state == 1) {
                                                $('#status').val("启用");
                                            }else {
                                                $('#status').val("停止");
                                            }
                                            if(data.createTime != "" && data.createTime != null) {
                                                $('#createTime').val(layui.util.toDateString(data.createTime));
                                            }

                                            form.render();
                                    });
                                }
                            });


                        }
                    }
                })

            } else if (obj.event === 'accountDetail') {
                var mchId = data.mchId;
                location.href = layui.setter.baseLocal + "merchant/list/account_view/mchId=" + mchId;
            } else if (obj.event === 'passageConfig') {
                if (data.kind == 2) {
                    layer.msg('私有账户,无需设置!');
                    //layer.alert("私有账户,无需设置!",{title: '提示'});
                    return;
                }
                location.href = layui.setter.baseLocal + "merchant/passage//mchId=" + data.mchId;
            } else if (obj.event === 'edit') {
                var mchId = data.mchId;
                admin.req({
                    type: 'get',
                    url: layui.setter.baseUrl + '/mch_info/get',
                    data: {
                        mchId : mchId
                    },
                    error: function(err){
                        layer.alert(JSON.stringify(err.field), {
                            title: '错误提示'
                        })
                    },
                    success: function(res){
                        if(res.code == 0){
                            // 2 赋值
                            data =res.data;

                            admin.popup({
                                title: '修改商户'
                                , area: ['410px', '490px']
                                , id: 'LAY-popup-content-edit'
                                , success: function (layero, index) {
                                    view(this.id).render('merchant/list/mch_edit').done(function () {
                                        form.render(null, 'layuiadmin-app-form-list');
                                        var body =layer.getChildFrame('body',index);
                                        body.find('#email').val(data.email);
                                        console.log(body)
                                        console.log(data.email)
                                        form.val("layuiadmin-app-form-list", {
                                            "email":data.email// "name": "value"
                                            ,"mobile": data.mobile
                                            ,"mchRate":data.mchRate
                                            ,"mchId" : data.mchId
                                        })
                                        if(res.data.kind == 1) {
                                            $("input[name='kind'][value='1']").attr("checked",true);
                                        }else if(res.data.kind == 2) {
                                            $("input[name='kind'][value='2']").attr("checked",true);
                                        }
                                        if(res.data.state == 1) {
                                            $("input[name='state'][value='1']").attr("checked",true);
                                        }else {
                                            $("input[name='state'][value='0']").attr("checked",true);
                                        }
                                        form.render();

                                        //监听提交
                                        form.on('submit(userEdit)', function (data) {
                                            if(data.field.state == "1") {
                                                data.field.state = "1";
                                            } else {
                                                data.field.state = "0";
                                            }
                                            admin.req({
                                                type: "POST",
                                                url: layui.setter.baseUrl + '/mch_info/update',
                                                data: data.field,
                                                success: function (res) {
                                                    if (res.code == 0) {
                                                        /*layer.alert(res.msg,{title: "保存成功"},function(index){
                                                            table.reload('tableReload');
                                                            layer.closeAll();
                                                        })*/
                                                        layer.msg('保存成功', {
                                                            offset: '15px'
                                                            ,icon: 1
                                                            ,time: 1000
                                                        }, function(){
                                                            table.reload("tableReload");
                                                            layer.closeAll();
                                                        });
                                                    } else {
                                                        /*layer.alert(res.msg, {
                                                            title: '保存失败'
                                                        });*/
                                                        layer.msg('保存失败', {
                                                            offset: '15px'
                                                            ,icon: 1
                                                            ,time: 1000
                                                        });
                                                    }
                                                }
                                            });
                                        });
                                        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                                    });
                                }
                            });
                        }
                    }
                })
            }
            });
            // 搜索
            var $ = layui.$, active = {
                reload: function () {
                    var mchId = $('#mchId').val();
                    var type = $("#type").val();
                    //执行重载
                    table.reload('tableReload', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        , where: {
                            mchId: mchId,
                            kind: type
                        }
                    });
                }
                ,add: function (othis) {
                    admin.popup({
                        title: '添加商户'
                        , area: ['410px', '508px']
                        , id: 'LAY-popup-content-add'
                        , success: function (layero, index) {
                            view(this.id).render('merchant/list/mch_add').done(function () {
                                form.render(null, 'layuiadmin-app-form-list');
                                //监听提交
                                form.on('submit(userAdd)', function (data) {
                                     if(data.field.state == "1") {
                                    data.field.state = "1";
                                } else {
                                    data.field.state = "0";
                                }
                                    admin.req({
                                        type: "POST",
                                        url: layui.setter.baseUrl + '/mch_info/add',
                                        data: data.field,
                                        success: function (res) {
                                            if (res.code == 0) {
                                                /*layer.alert(res.msg,{title: "保存成功"},function(index){
                                                    table.reload('tableReload');
                                                    layer.closeAll();
                                                })*/
                                                layer.msg('保存成功', {
                                                    offset: '15px'
                                                    ,icon: 1
                                                    ,time: 1000
                                                }, function(){
                                                    table.reload("tableReload");
                                                    layer.closeAll();
                                                });
                                            } else {
                                                /*
                                                layer.alert(res.msg, {
                                                    title: '保存失败'
                                                });*/
                                                layer.msg(res.msg, {
                                                    offset: '15px'
                                                    ,icon: 1
                                                    ,time: 1000
                                                });
                                            }
                                        }
                                    });
                                });
                                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                            });
                        }
                    });
                }

            };
            $('#search').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

            $('.layui-btn.layuiadmin-btn-list').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

            // 渲染表单
            form.render();

    });
</script>